<?php	
	/**
	 * 操作订单表模型
	 */
	 class GoodsOrderModel extends Model{
	 	
		public $table = 'goods_order';
		
		public $validate = array(
			array('order_sn','nonull','订单号不能为空',2,3),
			array('take_man','nonull','收货人不能为空',2,3),
			array('take_addr','nonull','收货地址不能为空',2,3),
			array('count_price','nonull','总价格不能为空',2,3),
			array('users_uid','nonull','您还没有登陆',2,3),
		);
		
		public $auto = array(
			array('order_time','time','function',2,3),
		);
		
		/**
		 * 生成一条订单方法
		 */
		public function createOrder(){
			//重组订单数据
			//得到当前购物车内所有商品信息
			$goods = Cart::getGoods();
			//收化人地址ID
			$addrid = Q('post.addrid');
			//选择的商品
			$goodsIds = Q('post.orderList');
			
			$address = K('ReceiveAddr')->takeAddr($addrid);
			
			$data = $this->reloadData($address);
			if(!$this->create($data)) return false;
			$data = $this->data;
			//返回生成订单的ID
			$orderId = $this->add($data);
			if($orderId){
				session('iswrite',1);
				$orderData['orderid'] = $orderId;
				$orderData['order_sn'] = $data['order_sn'];
				$orderData['orderprice'] = Q('post.goods_count_price',0,'intval');
				return $orderData;
			}else{
				$this->error = '生成订单失败';
				return false;	
			}
			
		}
		
		/**
		 * 返回当前订单信息当前订单商品列表明细
		 */
		public function orderDetails($sn){
			//返回当前订单
			$order = $this->where("order_sn='{$sn}'")->find();
			$oid = $order['oid'];
			//查询相应的订单列表中的商品
			$order['goods'] = M()->join('__order_list__ ol JOIN __goods__ g ON ol.goods_gid=g.gid')->where("ol.order_oid={$oid}")->all();			
			foreach($order['goods'] as $k=>$v){
				$order['goods'][$k]['goods_combinestr'] = explode(',',$v['goods_combinestr']);
			}
			return $order;
			
		}
		
		
		
		
		/**
		 * 重组订单数据
		 * @return array 最终订单数据
		 */
		private function reloadData($address){
			//组一个订单表的数据
			$data = array();
			//生成一个订单ID
			$data['order_sn'] = Cart::getOrderId();
			//收货人
			$data['take_man'] = $address['re_name'];
			//收货地址
			$data['take_addr'] = $address['address'].' '.$address['re_phone'].' '.$address['re_zipcode'];
			//总价格
			$data['count_price'] = number_format(Q('post.goods_count_price',0,'intval'),2);
			//订单状态
			$data['order_status'] = '未付款';
			//用户ID
			$data['users_uid'] = session('uid');
//			p($data);die;
			return $data;
		}
		
		
		
		/**
		 * 按相应订单状态查询订单
		 */
		public function getStatusOrder($status){
			$data = array();	
			if($status=='所有订单'){
				$count = $this->count();
				$page = new Page($count,15,10);
				$data['orderlists'] = $this->order('order_time desc')->limit($page->limit())->all();
				$data['page'] = $page->show(2);
			}else{
				$status = 'order_status='."'{$status}'";
				$count = $this->where("{$status}")->count();
				$page = new Page($count,15,10);
				$data['orderlists'] = $this->where("{$status}")->order('order_time desc')->limit($page->limit())->all();
				$data['page'] = $page->show(2);
			}
			
			return $data;
		}
		
		/**
		 * 返回当前的用户中心相应用户的所有订单包含商品图片
		 */
		 public function getUserOrderList(){
		 	$uid = session('uid');
			$status = Q('post.status','','strval');
			
			//显示所有定单
			if(!$status){
				$orders = $this->where("users_uid={$uid}")->order('order_time desc')->all();
			}else{
				$status = "'".str_replace(',',"','",$status)."'";
				//按条件筛选订单
				$orders = $this->where("users_uid={$uid} AND order_status IN({$status})")->order('order_time desc')->all();
			}
			return $orders;
		 }
		
		
	 }
	